// pages/purchase/purchase.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    titleBarHeight:"",
    add:"添加地址",//地址
    prc:"",//价格
    list:"",//数据列表
    dis:true,
    zhedis:true
  },
  //点击返回上一页
  back() {
    wx.navigateBack({
      delta: 1
    })
  },
  //点击我要寄存
  deps(){
    this.setData({
      dis:!this.data.dis
    })
   
  },
  addf(){
    wx.setStorageSync('type', 2)
    wx.navigateTo({
      url: '../address/address',
    })
  },
  //点击支付
  but(){
    let str = ''
    this.data.list.map(item => {
      if (str == '') {
        str += `${item.id}/${item.choice_num}`
      } else {
        str += `,${item.id}/${item.choice_num}`
      }
    })
    console.log(this.data.add)
    if (this.data.add=="添加地址"){
      wx.showToast({

        title: '请添加地址',

        icon: 'success',

        duration: 2000//持续的时间

      })
    }else{
      let that=this
      app.request({
        url: '/owtOrder/order',
        data:{
          'totalPrice':this.data.prc,
          'info':str,
          'addressid':this.data.add.id,
          'behaviour':2
        },
        method:'post',
        success: function (res) {
       
          wx.requestPayment(
            {

              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data._package,
              signType: res.data.signType,
              paySign: res.data.paySign,
              total_fee: res.data.total_fee,
              success: function (res) {
                that.setData({
                  zhedis:false
                })
               
              },
              fail: function (res) { },
              complete: function (res) { }
            })
        }

      })
    }
    
  },
  //点击关闭弹传
  guanbi(){
   
 wx.navigateTo({
                  url: '../order/order',
                })
  },
  buts(){
    let str = ''
    this.data.list.map(item => {
      if (str == '') {
        str += `${item.id}/${item.choice_num}`
      } else {
        str += `,${item.id}/${item.choice_num}`
      }
    })
    if (this.data.add=="添加地址"){
      wx.showToast({

        title: '请选择地址',

        icon: 'success',

        duration: 2000//持续的时间

      })
    }else{
      let that=this
      app.request({
        url: '/owtOrder/order',
        data:{
          'totalPrice':this.data.prc,
          'info':str,
          'addressid':this.data.add.id,
          'behaviour':1
        },
        method:'post',
        success: function (res) {
          wx.requestPayment(
            {

              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data._package,
              signType: res.data.signType,
              paySign: res.data.paySign,
              total_fee: res.data.total_fee,
              success: function (res) {
                that.setData({
                  dis:!that.data.dis,
                  zhedis:false
                })
              },
              fail: function (res) { },
              complete: function (res) { }
            })
        }

      })
    }
    
  },
  /**
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    

    let that = this;
    let arrval = wx.getStorageSync('arr')
    let newarr = []
    arrval.forEach(item =>{
      item.choice_num = 1
    })
    arrval.map(item =>{
      if(newarr.findIndex(items=>item.id == items.id) == -1){
        newarr.push(item)
      } else {
        let ind = newarr.findIndex(items=>item.id == items.id);
        newarr[ind].choice_num++
      }
    })
    console.log(newarr,9999)
    that.setData({
      add: wx.getStorageSync('addr') == "" ? "添加地址" : wx.getStorageSync('addr'),
      prc: wx.getStorageSync('pic'),
      list: newarr

    })
    console.log(that.data.list)
  },
  //点击增加
  add(e) {
    if (wx.getStorageSync('tk') == '123'){
      let index = e.currentTarget.dataset.id
console.log(e)
      let money = 0
      
      this.data.list[e.currentTarget.dataset.index].choice_num = this.data.list[e.currentTarget.dataset.index].choice_num + 1
      this.data.list.map(item => {
        money += item.choice_num * item.price;
      })
      this.setData({
        list: this.data.list,
        geshu: this.data.geshu + 1,
        datas: this.data.datas,
        prc: money
      })
    }else{
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }
    
    
  },
  adds(e){
    if (wx.getStorageSync('tk') == '123'){
      let index = e.currentTarget.dataset.id
      console.log(index)
      console.log(e)
      if (this.data.list[e.currentTarget.dataset.index].choice_num == 0) {
        this.data.list[e.currentTarget.dataset.index].choice_num = 0
        this.setData({
          list: this.data.list
        })
      }
      else {
        let money = 0;
        
        this.data.list[e.currentTarget.dataset.index].choice_num--
        if(!this.data.list[e.currentTarget.dataset.index].choice_num){
          let ind = this.data.list.findIndex(item=>item.id == this.data.list[e.currentTarget.dataset.index].id)
          this.data.list.splice(ind,1)
        }
        this.data.list.map(item => {
          money += item.choice_num * item.price;
        })
        this.setData({
          list: this.data.list,
          geshu: this.data.geshu - 1,
          prc: money
  
        })
      }
    }else{
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'CASKHOME',
      path: '/pages/otw/otw',
      imageUrl: wx.getStorageSync('fenx')//自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG。显示图片长宽比是 5:4。
    }
  }
})